<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
             :close-on-press-escape="false" width="40%">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item label="代肝内容：" prop="content">
        <el-input
          type="textarea"
          :rows="2"
          v-model="dataForm.content"
          placeholder="请输入代肝内容"
          style="width: 90%"
        ></el-input>
      </el-form-item>
      <el-form-item label="时效：" prop="finishTime">
<!--        <el-input v-model="dataForm.finishTime" placeholder="请输入时效" style="width: 90%"></el-input>-->
        <el-date-picker
          v-model="dataForm.finishTime"
          type="datetime"
          placeholder="选择日期时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          style="width: 90%"
        />
      </el-form-item>
      <el-form-item label="费用：" prop="money">
        <el-input v-model="dataForm.money" placeholder="请输入费用" style="width: 90%"></el-input>
      </el-form-item>
      <el-form-item label="接单人：" prop="assign">
        <!--        <el-input v-model="dataForm.assign" placeholder="接单人"></el-input>-->
        <el-select v-model="dataForm.assign" clearable placeholder="请选择接单人" style="width: 90%">
          <el-option
            v-for="item in options"
            :label="item.username"
            :value="item.username">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="游戏账号：" prop="gameUsername">
        <el-input v-model="dataForm.gameUsername" placeholder="请输入游戏账号" style="width: 90%"></el-input>
      </el-form-item>
      <el-form-item label="游戏密码：" prop="gamePassword">
        <el-input v-model="dataForm.gamePassword" placeholder="请输入游戏密码" style="width: 90%"></el-input>
      </el-form-item>
      <el-form-item label="区服：" prop="gameArea">
        <el-input v-model="dataForm.gameArea" placeholder="请输入区服" style="width: 90%"></el-input>
      </el-form-item>
      <el-form-item label="联系方式：" prop="wxQqPhone">
        <el-input v-model="dataForm.wxQqPhone" placeholder="请输入联系方式" style="width: 90%"></el-input>
      </el-form-item>
      <el-form-item label="支付方式：" prop="payMethod">
        <!--        <el-input v-model="dataForm.payMethod" placeholder="可选 微信 支付宝 自付款" style="width: 90%"></el-input>-->
        <el-select v-model="dataForm.payMethod" clearable placeholder="请选择支付方式" style="width: 90%">
          <el-option
            v-for="item in optionsPayment"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="意外情况：" prop="accident">
        <el-input v-model="dataForm.accident" placeholder="请输入意外情况" style="width: 90%"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);

let options = ref([
  {
    username:''
  }
]);

let optionsPayment = ref([
  {
    label: "微信",
    value: "微信"
  }, {
    label: "支付宝",
    value: "支付宝"
  }, {
    label: "自付款",
    value: "自付款"
  },
]);

const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  content: "",
  finishTime: "",
  money: "",
  assign: "",
  status: "",
  result: "",
  createTime: "",
  updateTime: "",
  gameUsername: "",
  gameArea: "",
  gamePassword: "",
  wxQqPhone: "",
  payMethod: "",
  accident: ""
});

const rules = ref({
  content: [
    {required: true, message: "必填项不能为空", trigger: "blur"}
  ],
  // finishTime: [
  //   { required: true, message: "必填项不能为空", trigger: "blur" }
  // ],
  money: [
    {required: true, message: "必填项不能为空", trigger: "blur"}
  ],
  // assign: [
  //   { required: true, message: "必填项不能为空", trigger: "blur" }
  // ],
  gameUsername: [
    {required: true, message: "必填项不能为空", trigger: "blur"}
  ],
  gamePassword: [
    {required: true, message: "必填项不能为空", trigger: "blur"}
  ],
  wxQqPhone: [
    {required: true, message: "必填项不能为空", trigger: "blur"}
  ],
  payMethod: [
    {required: true, message: "必填项不能为空", trigger: "blur"}
  ]
  // accident: [
  //   { required: true, message: "必填项不能为空", trigger: "blur" }
  // ]
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  //初始化时就从后端查询用户名
  baseService.get("/receiveJobDemo/taskassignment/queryUser").then((res) => {
    options = res.data
  });

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/receiveJobDemo/taskassignment/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/receiveJobDemo/taskassignment", dataForm).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
